{extend name="index/public" /}

{block name="content"}
<form class="layui-form" action="" id="searchBox">
    <div class="layui-inline">
        {MyTag:client name="client" /}
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            {MyTag:game type='manage' /}
        </div>
    </div>
    <div class="layui-inline">
        <input type="text" class="layui-input" id="date" placeholder="转账日期">
        <input type="hidden" id="transfer_time" value="">
    </div>
    <div class="layui-inline">
        <select name="" id="balance_type" lay-filter="balance_type">
            <option value="">结账类型</option>
            <option value="1">公账</option>
            <option value="2">私账</option>
        </select>
    </div>
    <div class="layui-inline" style="width: 800px;">
        <button class="layui-btn searchBtn my-btn my-btn-sm" lay-submit="">搜索</button>
        <button class="layui-btn toolBtn my-btn my-btn-sm" type="button" id="add">生成结算单</button>
        <button class="layui-btn toolBtn my-btn my-btn-sm" type="button" id="get">已收件</button>
        <button class="layui-btn toolBtn my-btn my-btn-sm" type="button" id="receive">已核对</button>
        <button class="layui-btn toolBtn my-btn my-btn-sm" type="button" id="urge">催单</button>
        <button class="layui-btn toolBtn my-btn my-btn-sm" type="button" id="finish">已转账</button>
        <button class="layui-btn toolBtn my-btn my-btn-sm" type="button" id="export">导出</button>
    </div>
</form>

<table id="tableBox" lay-filter="tableBox"></table>

<script type="text/html" id="statusTpl">
    {{#  if(d.status === '生成中'){ }}
    <span class="layui-bg-cyan status1" val="{{ d.id }}" style="padding: 4px 6px;">{{ d.status }}</span>
    {{#  } else if(d.status === '已提交') { }}
    <span class="layui-bg-orange status2" val="{{ d.id }}" style="padding: 4px 6px;">{{ d.status }}</span>
    {{#  } else if(d.status === '已核对') { }}
    <span class="layui-bg-blue status3" val="{{ d.id }}" style="padding: 4px 6px;">{{ d.status }}</span>
    {{#  } else if(d.status === '已转账') { }}
    <span class="layui-bg-green status4" val="{{ d.id }}" style="padding: 4px 6px;">{{ d.status }}</span>
    {{#  } }}

    {{#  if(d.urge===1 && d.status == '已核对'){ }}
    <span class="layui-bg-red status5" style="padding: 4px 6px;">催单</span>
    {{#  } }}

    {{#  if(d.get===1 && d.status == '已提交'){ }}
    <span class="layui-bg-cyan status8" style="padding: 4px 6px;">已收件</span>
    {{#  } }}
</script>

<script type="text/html" id="typeTpl">
    {{#  if(d.server_price==0){ }}
    <div style="border-bottom: 1px solid #ccc; padding-top:3px;">
        {{ d.type1 }}
    </div>
    <div>
        {{ d.type2 }}
    </div>
    {{#  } else { }}
        <span style="line-height:62px;">服务器费用</span>
    {{#  } }}
</script>

<script type="text/html" id="payPriceTpl">
    {{#  if(d.server_price==0){ }}
    <div style="border-bottom: 1px solid #ccc; padding-top:3px;">
        {{ d.cps_pay_price }}
    </div>
    <div>
        {{ d.sdk_pay_price }}
    </div>
    {{#  } else { }}
    <div style="line-height:62px;">
        {{ d.server_price }}
    </div>
    {{#  } }}
</script>

<script type="text/html" id="totalPriceTpl">
    {{#  if(d.server_price==0){ }}
    <div style="border-bottom: 1px solid #ccc; padding-top:3px;">
        {{ d.cps_total_price }}
    </div>
    <div>
        {{ d.sdk_total_price }}
        <!--<i style="color: #F9F500; font-weight: bold; font-style: normal;">({{ d.sdk_finish_rate }})</i>-->
    </div>
    {{#  } else { }}
    <div style="line-height:62px;">
        {{ d.all_total_price }}
    </div>
    {{#  } }}
</script>

<script type="text/html" id="finishPriceTpl">
    <div class="sdk_rate" id="{{ d.id }}" sdk_finish_price="{{ d.sdk_finish_price }}" sdk_back_price="{{ d.sdk_back_price }}" sdk_finish_rate="{{ d.sdk_finish_rate }}" sdk_no_price="{{ d.sdk_no_price }}" style="cursor: pointer;">
        <div style="border-bottom: 1px solid #ccc; padding-top:3px;">
            {{ d.sdk_finish_price }}
        </div>
        <div>
            {{ d.sdk_finish_rate }}
        </div>
    </div>
</script>

<script type="text/html" id="tableBar">
    <button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="view">查看</button>
    {{# if(d.status == '已转账'){ }}
    <button class="layui-btn layui-btn-sm layui-btn-disabled" disabled="" lay-event="edit">编辑</button>
    <button class="layui-btn layui-btn-sm layui-btn-disabled" disabled="" lay-event="del">删除</button>
    {{#  }else{ }}
        <button class="layui-btn layui-btn-green layui-btn-sm status6" val="{{ d.id }}" lay-event="edit">编辑</button>
        <button class="layui-btn layui-btn-danger layui-btn-sm status7" val="{{ d.id }}" lay-event="del">删除</button>
    {{#  } }}
</script>
{/block}

{block name="css"}
<style>
    table {border-collapse: separate;}
    .layui-table-cell {height: 62px; line-height:62px;}
    td[data-field=type] > div {line-height: 28px;}
    td[data-field=pay_price] > div {line-height: 28px;}
    td[data-field=total_price] > div {line-height: 28px;}
    td[data-field=finish_price] > div {line-height: 28px;}
    thead .layui-table-cell {line-height: 28px; height: 28px;}
</style>
{/block}

{block name="js"}
<script>
    layui.use(['table', 'laydate'], function(){
        var table = layui.table
            ,$ = layui.$
            ,form = layui.form
            ,load = null
            ,laydate = layui.laydate
            ,active = {
                reload: function () {
                    var jsonObj = {
                        "game_id": $('#game_id').val()
                        ,"transfer_time": $('#transfer_time').val()
                        ,"balance_type": $('#balance_type').val()
                        ,"game_server": $('#game_server').find('.my-btn').attr('val')
                        ,"game_publish": $('#game_publish').find('.my-btn').attr('val')
                        ,"game_platform": $('#game_platform').find('.my-btn').attr('val')
                        ,"game_client": $('#game_client').find('.my-btn').attr('val')
                    };

                    table.reload('tableId', {
                        page: {curr: 1}
                        ,where: jsonObj
                        ,height: 'full-100'
                    })
                },
            };

        //表格初始化
        table.render({
            elem: '#tableBox'
            ,height: 'full-100'
            ,url: '{:url("AuditCpMng/index")}'
            ,page: true
            ,limit: 10
            ,limits: [10,20,40,80]
            ,loading:true
            ,id:'tableId'
            ,done: function (res, curr, count) {
                layer.close(load);
                $('#layui-loading').hide();

                if(layui.data('auditCpMngPrompt').onOff==undefined){
                    var add = $('#add');
                    var get = $('#get');
                    var receive = $('#receive');
                    var urge = $('#urge');
                    var finish = $('#finish');
                    setTimeout(function () {layer.tips('商务进入该页面，查询游戏分成生成结算单', add, {tips: 3, tipsMore:true, time: 5000});}, 0);
                    setTimeout(function () {layer.tips('财务收到商务申请书，选择"已收到"账单加入"已收件"标注', get, {tips: 3, tipsMore:true, time: 5000});}, 5000);
                    setTimeout(function () {layer.tips('商务收到CP结算单后，选择"已收到"->"已核对"', receive, {tips: 3, tipsMore:true, time: 5000});}, 10000);
                    setTimeout(function () {layer.tips('商务选择"已核对"账单加入"催单"标注，可提醒财务优先转账', urge, {tips: 3, tipsMore:true, time: 5000});}, 15000);
                    setTimeout(function () {layer.tips('财务真实转账后，选择"已核对"->"已转账"', finish, {tips: 3, tipsMore:true, time: 5000});}, 20000);
                    layui.data('auditCpMngPrompt', {key: 'onOff',value: 1});
                }
            }
            ,cols: [[
                {type:'checkbox', fixed: 'left'}
                ,{field:'id', title: 'ID', width: 60, sort:true}
                ,{field:'game_id', title: '游戏ID', width: 130, sort:true, style:'background-color: #6495ed; color: #fff;'}
                ,{field:'daytime', title: '对账日期', width: 170, sort:true, style:'background-color: #6495ed; color: #fff;'}
                ,{field:'balance_type', title: '类型', width: 60}
                ,{field:'type', title: '费用', width: 70, templet: '#typeTpl'}
                ,{field:'pay_price', title: '充值金额', width: 90, templet: '#payPriceTpl', style:'background-color: #6495ed; color: #fff;'}
                ,{field:'pay_rate', title: '分成', width: 60}
                ,{field:'channel_rate', title: '通道', width: 60}
                ,{field:'tax_rate', title: '税率', width: 60}
                ,{field:'total_price', title: '结算金额', width: 90, templet: '#totalPriceTpl', style:'background-color: #6495ed; color: #fff;'}
                ,{field:'finish_price', title: 'sdk需回款', width: 90, templet: '#finishPriceTpl'}
                ,{field:'all_total_price', title: '合计', width: 90}
                ,{field:'invoice_type', title: '发票', width: 80, sort:true}
                ,{field:'balance_time', title: '结算日期', width: 98, sort:true}
                ,{field:'transfer_time', title: '转账日期', width: 98, sort:true}
                ,{field:'status', title: '状态', width: 136, templet: '#statusTpl', fixed: 'right'}
                ,{title: '操作', align:'center', width: 170, toolbar:'#tableBar', fixed: 'right'}
            ]]
        });

        //修改状态
        form.on('switch(status)', function(obj){
            var status = obj.elem.checked ? 1 : 0
                ,load = layer.load();
            $.post('{:url("AuditCpMng/edit")}',{id:obj.value, status:status}, function(json){
                layer.close(load);
                if(json.code==0){
                    layer.msg(json.msg, {icon: 1, time:1000})
                }else{
                    layer.msg(json.msg, {icon: 0, time:1000});
                }
            }, 'JSON').error(function(){
                layer.close(load);
                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
            });
        });

        //监听搜索栏
        $('#searchBox .toolBtn').on('click', function(){
            if($(this).attr('id')=='add'){
                layer.open({
                    type: 2
                    ,title: '添加'
                    ,area: ['100%', '100%']
                    ,content: '{:url("AuditCpMng/add")}'
                });
            }else if($(this).attr('id')=='get'){
                var checkStatus = table.checkStatus('tableId')
                    ,data = checkStatus.data
                    ,str = '';

                if(data.length==0){
                    layer.msg('请选择要修改的条目');
                    return false;
                }

                for(key in data){
                    str += data[key].id+',';
                }
                var load = layer.load();
                $.post('{:url("AuditCpMng/get")}',{id: str.substring(0, str.length-1)}, function(json){
                    layer.close(load);
                    if(json.code==0){
                        for (key in data){
                            $('.status2[val="'+data[key].id+'"]').next('.status8').remove();
                            $('.status2[val="'+data[key].id+'"]').after(' <span class="layui-bg-cyan status8" style="padding: 4px 6px;">已收件</span>');
                        }
                        layer.msg(json.msg, {icon: 1, time:1000})
                    }else{
                        layer.msg(json.msg, {icon: 0, time:1000});
                    }
                }, 'JSON').error(function(){
                    layer.close(load);
                    layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                });
            }else if($(this).attr('id')=='receive'){
                var checkStatus = table.checkStatus('tableId')
                    ,data = checkStatus.data
                    ,str = '';

                if(data.length==0){
                    layer.msg('请选择要修改的条目');
                    return false;
                }

                for(key in data){
                    str += data[key].id+',';
                }
                var load = layer.load();
                $.post('{:url("AuditCpMng/receive")}',{id: str.substring(0, str.length-1)}, function(json){
                    layer.close(load);
                    if(json.code==0){
                        for (key in data){
                            $('.status2[val="'+data[key].id+'"]').siblings().remove();
                            $('.status2[val="'+data[key].id+'"]').text('已核对').addClass('layui-bg-blue status3').removeClass('layui-bg-orange status2');
                        }
                        layer.msg(json.msg, {icon: 1, time:1000})
                    }else{
                        layer.msg(json.msg, {icon: 0, time:1000});
                    }
                }, 'JSON').error(function(){
                    layer.close(load);
                    layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                });
            }else if($(this).attr('id')=='finish'){
                var checkStatus = table.checkStatus('tableId')
                    ,data = checkStatus.data
                    ,str = '';

                if(data.length==0){
                    layer.msg('请选择要修改的条目');
                    return false;
                }

                for(key in data){
                    str += data[key].id+',';
                }
                var load = layer.load();
                $.post('{:url("AuditCpMng/finish")}',{id: str.substring(0, str.length-1)}, function(json){
                    layer.close(load);
                    if(json.code==0){
                        for (key in data){
                            $('.status3[val="'+data[key].id+'"]').siblings().remove();
                            $('.status3[val="'+data[key].id+'"]').text('已转账').addClass('layui-bg-green status4').removeClass('layui-bg-blue status3');
                            $('.status6[val="'+data[key].id+'"]').addClass('layui-btn-disabled').removeClass('layui-btn-green').attr('disabled', true);
                            $('.status7[val="'+data[key].id+'"]').addClass('layui-btn-disabled').removeClass('layui-btn-danger').attr('disabled', true);
                        }
                        layer.msg(json.msg, {icon: 1, time:1000})
                    }else{
                        layer.msg(json.msg, {icon: 0, time:1000});
                    }
                }, 'JSON').error(function(){
                    layer.close(load);
                    layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                });
            }else if($(this).attr('id')=='urge'){
                var checkStatus = table.checkStatus('tableId')
                    ,data = checkStatus.data
                    ,str = '';

                if(data.length==0){
                    layer.msg('请选择要修改的条目');
                    return false;
                }

                for(key in data){
                    str += data[key].id+',';
                }
                var load = layer.load();
                $.post('{:url("AuditCpMng/urge")}',{id: str.substring(0, str.length-1)}, function(json){
                    layer.close(load);
                    if(json.code==0){
                        for (key in data){
                            $('.status3[val="'+data[key].id+'"]').next('.status5').remove();
                            $('.status3[val="'+data[key].id+'"]').after(' <span class="layui-bg-red status5" style="padding: 4px 6px;">催单</span>');
                        }
                        layer.msg(json.msg, {icon: 1, time:1000})
                    }else{
                        layer.msg(json.msg, {icon: 0, time:1000});
                    }
                }, 'JSON').error(function(){
                    layer.close(load);
                    layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                });
            }else if($(this).attr('id')=='export'){
                var jsonStr = ''
                    ,jsonObj = {
                        "game_id": $('#game_id').val()
                        ,"transfer_time": $('#transfer_time').val()
                        ,"balance_type": $('#balance_type').val()
                        ,"game_server": $('#game_server').find('.my-btn').attr('val')
                        ,"game_publish": $('#game_publish').find('.my-btn').attr('val')
                        ,"game_platform": $('#game_platform').find('.my-btn').attr('val')
                        ,"game_client": $('#game_client').find('.my-btn').attr('val')
                    };
                layer.msg('导出中....', {icon: 1, time: 2000});
                for(k in jsonObj){
                    jsonStr += k + '=' + jsonObj[k] + '&';
                }
                window.location.href = '{:url("AuditCpMng/export")}?'+jsonStr.substring(0, jsonStr.length-1);
            }
        })

        $('#searchBox .searchBtn').on('click', function(){
            load = layer.load();
            active['reload'].call(this);
            return false;
        });

        //监听搜索下拉菜单
        form.on('select(game_id)', function(data){
            load = layer.load();
            active['reload'].call(this);
        });

        //监听搜索下拉菜单
        form.on('select(balance_type)', function(data){
            load = layer.load();
            active['reload'].call(this);
        });

        //监听时间搜索
        laydate.render({
            elem: '#date'
            ,type: 'date'
            ,range: true
            ,done: function(value, date, endDate){
                $('#transfer_time').val(value);
                load = layer.load();
                active['reload'].call(this);
            }
        });

        //监听工具条
        table.on('tool(tableBox)', function(obj){
            var data = obj.data
            if(obj.event === 'view'){
                layer.open({
                    type: 1
                    ,title: false
                    ,area: '1000px;'
                    ,shadeClose: true
                    ,offset: '200px'
                    ,shade: 0
                    ,cancel: function(){
                        $.post('{:url("AuditCpMng/index")}',{id: data.id}, function(json){
                            obj.update({
                                fact_price: json.data[0].fact_price
                            })
                        }, 'JSON').error(function(){
                            layer.close(load);
                            layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                        });
                    }
                    ,content: '<div style="padding: 20px;"><table class="layui-table">' +
                            '<thead><th>类型</th><th>实际充值</th><th>差额</th></thead><tbody><tr>'
                                +'<td>CPS</td>'
                                +'<td>'+data.cps_fixed_price+'</td>'
                                +'<td>'+data.cps_diff_price+'</td>'
                            +'</tr>'
                            +'<tr>'
                                +'<td>联运</td>'
                                +'<td>'+data.sdk_fixed_price+'</td>'
                                +'<td>'+data.sdk_diff_price+'</td>'
                            +'</tr>'
                            +'<tr>'
                            +'<tr>'
                            +'<td>实结金额</td>'
                            +'<td colspan="3"><input type="text" style="margin-left:-10px;" value="'+data.fact_price+'" class="layui-input fact_price" val="'+data.id+'"></td>'
                            +'</tr>'
                            +'<td>回款金额</td>'
                            +'<td colspan="3">'+data.sdk_finish_price+' | '+data.sdk_back_price+'（'+data.sdk_finish_rate+'）'+'</td>'
                            +'</tr>'
                            +'<tr>'
                                +'<td>备注</td>'
                                +'<td colspan="3">'+data.remark+'</td>'
                            +'</tr></tbody></table></div>'
                });
            }else if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '编辑'
                    ,resize: true
                    ,area: ['100%', '100%']
                    ,content: '{:url("AuditCpMng/edit")}?id='+data.id
                    ,cancel: function(){
                        $.post('{:url("AuditCpMng/index")}',{id: data.id}, function(json){
                            obj.update({
                                cps_pay_price: json.data[0].cps_pay_price
                                ,sdk_pay_price: json.data[0].sdk_pay_price
                                ,cps_fixed_price: json.data[0].cps_fixed_price
                                ,sdk_fixed_price: json.data[0].sdk_fixed_price
                                ,channel_rate: json.data[0].channel_rate
                                ,tax_rate: json.data[0].tax_rate
                                ,pay_rate: json.data[0].pay_rate
                                ,cps_total_price: json.data[0].cps_total_price
                                ,sdk_total_price: json.data[0].sdk_total_price
                                ,all_total_price: json.data[0].all_total_price
                                ,invoice_type: json.data[0].invoice_type
                                ,balance_time: json.data[0].balance_time
                                ,transfer_time: json.data[0].transfer_time
                                ,status: json.data[0].status
                                ,cps_diff_price: json.data[0].cps_diff_price
                                ,sdk_diff_price: json.data[0].sdk_diff_price
                                ,remark: json.data[0].remark
                                ,urge: json.data[0].urge
                                ,balance_type: json.data[0].balance_type
                            })
                        }, 'JSON').error(function(){
                            layer.close(load);
                            layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                        });
                    }
                });
            }else if(obj.event === 'del'){
                layer.confirm('确定要删除吗？', {offset: '250px'}, function(index){
                    var load = layer.load();
                    $.post('{:url("AuditCpMng/del")}',{id: data.id}, function(json){
                        layer.close(load);
                        if(json.code==0){
                            obj.del();
                            layer.msg(json.msg, {icon: 1, time:1000})
                        }else{
                            layer.msg(json.msg, {icon: 0, time:1000});
                        }
                    }, 'JSON').error(function(){
                        layer.close(load);
                        layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                    });
                });
            }
        });


        $('body').on('click', '.sdk_rate', function(){
            layer.open({
                type: 2
                ,title: '查看'
                ,offset: '100px'
                ,resize: true
                ,area: ['1200px', '700px']
                ,content: '{:url("AuditCpMng/view")}?'
                    +'id='+$(this).attr('id')
                    +'&sdk_finish_price='+$(this).attr('sdk_finish_price')
                    +'&sdk_back_price='+$(this).attr('sdk_back_price')
                    +'&sdk_finish_rate='+$(this).attr('sdk_finish_rate')
                    +'&sdk_no_price='+$(this).attr('sdk_no_price')
            });
        })

        $('body').on('blur', '.fact_price', function(){
            $.post('{:url("AuditCpMng/edit2")}',{id: $(this).attr('val'), fact_price: $(this).val()}, function(json){
                if(json.code==0){
                    layer.msg(json.msg, {icon: 1, time:1000})
                }else{
                    layer.msg(json.msg, {icon: 0, time:1000});
                }
            }, 'JSON').error(function(){
                layer.close(load);
                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
            });
        });
    });
</script>
{/block}